//
//  TYZRefreshCollectionViewController.m
//  AVfoundationDemo
//
//  Created by TangBin on 2019/8/22.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZRefreshCollectionViewController.h"

@interface TYZRefreshCollectionViewController ()
{
    CGFloat ignoredScrollViewContentInsetTop_;
    CGFloat ignoredScrollViewContentInsetBottom_;
}

/**
 是否第一次，进入界面就显示下拉，默认是NO
 */
@property (nonatomic, assign) BOOL isFirstRefresh;

/**
 是否正在加载数据。默认为NO
 */
@property (nonatomic, assign, readwrite) BOOL isRefreshing;
@end

@implementation TYZRefreshCollectionViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil firstShowRefresh:_isFirstRefresh];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil firstShowRefresh:(BOOL)firstShowRefresh
{
    _isFirstRefresh = firstShowRefresh;
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    return self;
}

- (id)init
{
    return [self initWithFirstShowRefresh:NO];
}

- (id)initWithFirstShowRefresh:(BOOL)firstShowRefresh
{
    _isFirstRefresh = firstShowRefresh;
    //    NSLog(@"firstShowRefresh=%d", firstShowRefresh);
    self = [super init];
    if (self)
    {
        
    }
    return self;
}

- (void)initWithVar
{
    [super initWithVar];
    
    _pageId = 1;
    _isRefreshing = NO;
    self.isAllFinished = NO;
    
}

- (void)initWithNavBar
{
    [super initWithNavBar];
    
}

- (void)initWithSubView
{
    [super initWithSubView];
    
}

- (void)initWithBasicCollectionView
{
    [super initWithBasicCollectionView];
    
    // 1.下拉刷新(进入刷新状态就会调用self的doRefreshData)
    // dataKey 用于存储刷新事件，可以保证不同的界面拥有不同的刷新时间
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(doRefreshData)];
    header.ignoredScrollViewContentInsetTop = ignoredScrollViewContentInsetTop_;
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"正在刷新中" forState:MJRefreshStateRefreshing];
    //    header.stateLabel.font = [UIFont systemFontOfSize:15];
    //    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    //    header.stateLabel.textColor = [UIColor redColor];
    self.baseCollectionView.mj_header = header;
    //    __unsafe_unretained __typeof(self)weakSelf = self;
    NSLog(@"_isFirstRefresh=%d", _isFirstRefresh);
    if (_isFirstRefresh)
    {
        //#warning 自动刷新(已进入程序就下拉刷新)
        [self.baseCollectionView.mj_header beginRefreshing];
    }
    
    // 2.上拉加载更多(进入刷新状态就会调用self的doMoreRefreshData)
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(doMoreRefreshData)];
    footer.ignoredScrollViewContentInsetBottom = ignoredScrollViewContentInsetBottom_;
    [footer setTitle:@"上拉加载更多数据" forState:MJRefreshStateIdle];
    [footer setTitle:@"松开加载更多数据" forState:MJRefreshStatePulling];
    [footer setTitle:@"正在加载中" forState:MJRefreshStateRefreshing];
    self.baseCollectionView.mj_footer = footer;
    
    [self hiddenFooterView:YES];
}

- (void)setIgnoredScrollViewContentInsetTop:(CGFloat)ignoredScrollViewContentInsetTop
{
    ignoredScrollViewContentInsetTop_ = ignoredScrollViewContentInsetTop;
    self.baseCollectionView.mj_header.ignoredScrollViewContentInsetTop = ignoredScrollViewContentInsetTop;
}

- (void)setIgnoredScrollViewContentInsetBottom:(CGFloat)ignoredScrollViewContentInsetBottom
{
    ignoredScrollViewContentInsetBottom_ = ignoredScrollViewContentInsetBottom;
    self.baseCollectionView.mj_footer.ignoredScrollViewContentInsetBottom = ignoredScrollViewContentInsetBottom;
}

- (void)beginRefreshing
{
    [self.baseCollectionView.mj_header beginRefreshing];
}

#pragma mark - setter and geter
- (BOOL)isRefreshing
{
    return (self.baseCollectionView.mj_header.isRefreshing && self.baseCollectionView.mj_footer.isRefreshing);
}

/**
 下拉刷新数据
 */
- (void)doRefreshData
{
    self.isAllFinished = NO;
    _pageId = 1;
    self.isHeadRefresh = YES;
}

/**
 上拉加载更多数据
 */
- (void)doMoreRefreshData
{
    self.isHeadRefresh = NO;
    if (self.isAllFinished)
    {// 所有数据都加载完了，就直接停止加载
        [self.baseCollectionView.mj_footer endRefreshing];
    }
}

/**
 结束所有刷新
 */
- (void)endAllRefreshing
{
    [self endHeaderRefreshing];
    
    [self endFooterRefreshing];
}

/**
 下拉刷新结束
 */
- (void)endHeaderRefreshing
{
    _isRefreshing = NO;
    [self.baseCollectionView.mj_header endRefreshing];
}

/**
 上拉刷新结束
 */
- (void)endFooterRefreshing
{
    _isRefreshing = NO;
    [self.baseCollectionView.mj_footer endRefreshing];
}

/**
 隐藏footer
 
 @param hidden hidden description
 */
- (void)hiddenFooterView:(BOOL)hidden
{
    [self.baseCollectionView.mj_footer setHidden:hidden];
}

/**
 隐藏header
 
 @param hidden hidden description
 */
- (void)hiddenHeaderView:(BOOL)hidden
{
    [self.baseCollectionView.mj_header setHidden:hidden];
}

@end

